﻿using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Camc.OutsourceProcession.ProduceTask.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Camc.OutsourceProcession.ProduceTask
{
	public class ProcessCardAppService : OutsourceProcessionAppServiceBase
	{
		private readonly IRepository<Order, long> _orderRepository;
		private readonly IRepository<SpecialTool, long> _specialToolRepository;
		private readonly IRepository<ProductLog, long> _ProductLogRepository;

		public ProcessCardAppService(IRepository<Order, long> orderRepository, IRepository<SpecialTool, long> specialToolRepository, IRepository<ProductLog, long> ProductLogRepository)
		{
			_orderRepository = orderRepository;
			_specialToolRepository = specialToolRepository;
			_ProductLogRepository = ProductLogRepository;
		}

		public ICollection<SpecialToolDto> GetAllSpecialToolByOrder(long orderId)
		{
			var specialTools = _specialToolRepository.GetAll().Where(c => c.OrderId == orderId).ToList();
			return ObjectMapper.Map<ICollection<SpecialToolDto>>(specialTools);
		}

		public void DeleteSpecialTool(long specialToolId)
		{
			_specialToolRepository.Delete(specialToolId);
		}

		public SpecialToolDto GetSpecialToolForEdit(long id)
		{
			var specialTool = _specialToolRepository.Get(id);
			return ObjectMapper.Map<SpecialToolDto>(specialTool);
		}

		

		public long CreateOrUpdateSpecialTool(SpecialToolDto input)
		{
			if (input.Id.HasValue)
			{
				var specialTool = _specialToolRepository.Get(input.Id.Value);

				specialTool.Length = input.Length;
				specialTool.LengthOfEdge = input.LengthOfEdge;
				specialTool.LengthOfHandle = input.LengthOfHandle;
				specialTool.Name = input.Name;
				specialTool.Explain = input.Explain;

				_specialToolRepository.Update(specialTool);
				return specialTool.Id;
			}
			else
			{
				var specialTool = new SpecialTool();
				specialTool = ObjectMapper.Map<SpecialTool>(input);
				return _specialToolRepository.InsertAndGetId(specialTool);
			}

		}

		public void SaveSpecialTools(List<SpecialToolDto> input)
		{
			foreach(var specialToolDto in input)
			{
				var specialTool = _specialToolRepository.Get(specialToolDto.Id.Value);

				specialTool.Length = specialToolDto.Length;
				specialTool.LengthOfEdge = specialToolDto.LengthOfEdge;
				specialTool.LengthOfHandle = specialToolDto.LengthOfHandle;
				specialTool.Name = specialToolDto.Name;
				specialTool.Explain = specialToolDto.Explain;

				_specialToolRepository.Update(specialTool);
			}
		}

		public string GetMaterials(long orderId)
		{
			return _orderRepository.GetAll().Where(c => c.Id == orderId).Select(c => c.Materials).FirstOrDefault();
		}

		public void UpdateMaterials(UpdateMaterialsDto input)
		{
			var order = _orderRepository.Get(input.Id);
			order.Materials = input.Materials;
			_orderRepository.Update(order);
		}

		public List<ComboboxItemDto> GetOrderState(long orderId)
		{
			System.Array values = System.Enum.GetValues(typeof(OrderState));

			var orderState = _orderRepository.Get(orderId).State;

			var allState = new List<ComboboxItemDto>();
			foreach (var value in values)
			{
				var state = new ComboboxItemDto(((int)value).ToString(),value.ToString());
				if ((int)orderState == (int)value)
				{
					state.IsSelected = true;
				}
				allState.Add(state);
			}

			return allState;
		}

		public void ChangeOrderState(UpdateOrderStateDto input)
		{
			var order = _orderRepository.Get(input.Id);
			var oldState = Enum.GetName(typeof(OrderState), order.State);
			var newState = Enum.GetName(typeof(OrderState), input.StateId);

			order.State = (OrderState)Enum.ToObject(typeof(OrderState), input.StateId);
			_orderRepository.Update(order);


			//todo:添加日志
			var ProductLog = new ProductLog();
			ProductLog.OrderId = input.Id;
			ProductLog.Operator = GetCurrentUser().UserName;
			ProductLog.Events = "任务状态从：[" + oldState + "] 变为 "+ "["+ newState + "]";

			_ProductLogRepository.Insert(ProductLog);


		}

		public void AddProductLog(ProductLogInput input)
		{
			var ProductLog = ObjectMapper.Map<ProductLog>(input);

			ProductLog.Operator = GetCurrentUser().UserName;

			_ProductLogRepository.Insert(ProductLog);
		}

		public ICollection<ProductLogOutput> GetAllOrderLogs(long orderId)
		{
			var ProductLogs = _ProductLogRepository.GetAll().Where(c => c.OrderId == orderId).OrderByDescending(c=>c.CreationTime).ToList();

			return ObjectMapper.Map<ICollection<ProductLogOutput>>(ProductLogs);

		}

		

	}
}
